#include "common.h"

#include "uievent.h"
#include "observer.h"
#include "uieventreceiver.h"
#include "ui.h"
#include "interactableui.h"

#include "button.h"

Button::Button( DWORD posX, DWORD posY, DWORD width, DWORD height )
:	InteractableUI()
,	_isButtonClicked( false )
,	_isButtonPushed( false )
,	_pTexture( NULL )
{
	SetStartPos( posX, posY );
	SetRect( 0, 0, width, height );
}

Button::~Button()
{
	if( _pTexture )
	{
		_pTexture->Release();
		_pTexture = NULL;
	}

	InteractableUI::~InteractableUI();
}

void Button::ActualDraw()
{
	D3DXVECTOR3 pos = D3DXVECTOR3( GetStartPos().x, GetStartPos().y, 0 );
	UI::GetSprite()->Draw( _pTexture, &GetRect(), NULL, &pos, 0xFFFFFFFF );
}

void Button::SetImage( LPCWSTR filePath )
{
	LPDIRECT3DDEVICE9 device = Core::GetInstance().GetD3dDevice();

	if( FAILED( D3DXCreateTextureFromFile( device, filePath, &_pTexture ) ) )
	{
		// error
		assert( false );
	}
}

void Button::OnMouseOutOfPosition()
{
	RECT rect = GetRect();
	int height = rect.bottom - rect.top;
	SetRect( 0, BUTTON_OUTOFPOS*height, rect.right-rect.left, (BUTTON_OUTOFPOS+1)*height );

	_isButtonPushed = false;
}

void Button::OnMouseInPosition()
{
	if( _isButtonPushed == false )
	{
		RECT rect = GetRect();
		int height = rect.bottom - rect.top;
		SetRect( 0, BUTTON_ONPOS*height, rect.right-rect.left, (BUTTON_ONPOS+1)*height );
	}
}

void Button::OnMouseLButtonDown()
{
	RECT rect = GetRect();
	int height = rect.bottom - rect.top;
	SetRect( 0, BUTTON_ONCLICK*height, rect.right-rect.left, (BUTTON_ONCLICK+1)*height );

	_isButtonPushed = true;
}

void Button::OnMouseLButtonUp()
{
	RECT rect = GetRect();
	int height = rect.bottom - rect.top;
	SetRect( 0, BUTTON_ONPOS*height, rect.right-rect.left, (BUTTON_ONPOS+1)*height );

	_isButtonPushed = false;
	_isButtonClicked = true;
}